package com.slkj.shilixiaoyuanapp;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.UserEntity;
import com.slkj.shilixiaoyuanapp.ui.main.MainActivity;
import com.slkj.shilixiaoyuanapp.ui.user.LoginActivity;
import com.slkj.shilixiaoyuanapp.util.AutoDensityUtils;

import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

public class StartPageActivity extends AppCompatActivity {
    private SharedPreferences sharedPreferences;
    ViewPager viewPager;
    TextView tvGo;
    int[] res = {R.drawable.welcome1,R.drawable.welcome2,R.drawable.welcome3};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        sharedPreferences = getSharedPreferences("welcome_info", MODE_PRIVATE);

        new Handler().postDelayed(() -> {
            if (UserRequest.getInstance().isLogin()) {
                int verson = sharedPreferences.getInt("verson",1);
                //新版本
                if (BuildConfig.VERSION_CODE > verson){
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putInt("verson",BuildConfig.VERSION_CODE);
                    editor.commit();
                    UserRequest.getInstance().exitLogon();
                }else {
                    goTo(MainActivity.class);
                }
            } else {
                //是否进入了欢迎页面
                boolean isWelcome = sharedPreferences.getBoolean("isWelcome", false);
                //版本更新就清除数据：

                if (isWelcome) {
                    goTo(LoginActivity.class);
                }else {
                    setRequestedOrientation(SCREEN_ORIENTATION_PORTRAIT);
                    AutoDensityUtils.setCustomDensity(this);
                    setContentView(R.layout.activity_start_page);
                    viewPager = findViewById(R.id.viewpager);
                    tvGo = findViewById(R.id.tv_go);
                    viewPager.setAdapter(new MyPagerAdapter());
                    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                        }

                        @Override
                        public void onPageSelected(int position) {
                            if (position == 2){
                                tvGo.setVisibility(View.VISIBLE);
                            }else {
                                tvGo.setVisibility(View.GONE);

                            }
                        }

                        @Override
                        public void onPageScrollStateChanged(int state) {

                        }
                    });
                    tvGo.setOnClickListener(v -> {
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putBoolean("isWelcome",true);
                        editor.commit();
                        goTo(LoginActivity.class);

                    });
                }
            }
        }, 1000);
        super.onCreate(savedInstanceState);

    }

    private void goTo(Class s) {
        Intent intent = new Intent(this, s);
        startActivity(intent);
        finish();
    }
    public class MyPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return res.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = LayoutInflater.from(StartPageActivity.this).inflate(R.layout.layout_welcome, container, false);
            ImageView imageView = view.findViewById(R.id.img);
            imageView.setImageResource(res[position]);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

    }
}
